package com.boot.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author luowenjie
 * @since 2021/11/23 19:31
 */
@Configuration
@EnableSwagger2
public class Swagger2Conf {
    @Bean
    public Docket createRestApi() {
        // 创建 Docket 对象
        return new Docket(DocumentationType.SWAGGER_2) // 文档类型，使用 Swagger2
                .apiInfo(this.apiInfo()) // 设置 API 信息
                // 扫描 Controller 包路径，获得 API 接口
                .select()
                /*
                RequestHandlerSelectors，配置要扫描接口的方式
                RequestHandlerSelectors.any()：扫描报的全部
                RequestHandlerSelectors.basePackage("com.lucas.controller")扫描指定的包
                RequestHandlerSelectors.none()都不扫描
                RequestHandlerSelectors.withClassAnnotation扫描类上的注解:例如：
                RequestHandlerSelectors.withClassAnnotation(RestController.class)GetMapping
                RequestHandlerSelectors.withMethodAnnotation扫描方法上的注解，例如:
                RequestHandlerSelectors.withMethodAnnotation(GetMapping.class)
                 */
                //.apis(RequestHandlerSelectors.basePackage("com.example.redis.rest"))
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                // 构建出 Docket 对象
                .build();
    }

    /**
     * 创建 API 信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("DEMO接口文档")
                .description("用于JAVA基础及springboot相关 练习")
                .version("1.0.0") // 版本号
                .contact(new Contact("罗文洁", "https://gitee.com/luo-wenjie12/spring-boot.git", "754633195@qq.com")) // 联系人
                .build();
    }
}
